package uit.com.project.iCampus.xmlparser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import uit.com.project.iCampus.model.User;
import uit.com.project.iCampus.model.UserOnline;

public class UserOnlineXMLHandler extends DefaultHandler {
	Boolean currentElement = false;
	String currentValue = null;
	private List<UserOnline> userOnlines = new ArrayList<UserOnline>();
	private UserOnline userOnline = null;
	private User user = null;

	/**
	 * @return the userOnlines
	 */
	public List<UserOnline> getUserOnlines() {
		return userOnlines;
	}

	/**
	 * @param userOnlines
	 *            the userOnlines to set
	 */
	public void setUserOnlines(List<UserOnline> userOnlines) {
		this.userOnlines = userOnlines;
	}

	/**
	 * @return the userOnline
	 */
	public UserOnline getUserOnline() {
		return userOnline;
	}

	/**
	 * @param userOnline
	 *            the userOnline to set
	 */
	public void setUserOnline(UserOnline userOnline) {
		this.userOnline = userOnline;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;
		String element = (qName.equals("") ? localName : qName);
		if (element.equals(XMLParserAndroidUtil.ELEMENT)) {
			if (attributes.getValue(0).equals("userOnline")) {
				userOnline = new UserOnline();
				userOnlines.add(userOnline);
			}
		} else if (element.equals("userOnline")) {
			userOnline = new UserOnline();
			userOnlines.add(userOnline);
		}else if (element.equals("user")){
			user = new User();
			userOnline.setUser(user);
		}
	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		currentElement = false;
		String element = (qName.equals("") ? localName : qName);
		/** set value */
		if (element.equalsIgnoreCase("latitude"))
			userOnline.setLatitude(Float.valueOf(currentValue));
		else if (element.equalsIgnoreCase("longitude"))
			userOnline.setLongitude(Float.valueOf(currentValue));
		else if (element.equalsIgnoreCase("fullName"))
			user.setFullName(currentValue);
		else if (element.equalsIgnoreCase("password"))
			user.setPassword(currentValue);
		else if (element.equalsIgnoreCase("username"))
			user.setUsername(currentValue);
	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String chars = (new String(ch).substring(start, start + length));
		if (currentElement) {
			currentValue = chars;
		}
	}
}
